package com.zc.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 通过注解定义切面
 * Created by zhangchi9 on 2016/11/7.
 */
@Component
@Aspect
public class AudienceAnnotation {
    /**
     * 定义切点
     */
    @Pointcut("execution(* com.zc.demo.di.interfaces.Performer.perform(..))")
    public void performance(){}

    @Before("performance()")
    public void takeSeats(){
        System.out.println("表演之_前获得座位");
    }
    @Before("performance()")
    public void turnOffCellPhones(){
        System.out.println("表演之_前关闭手机");
    }
    @After("performance()")
    public void after(){
        System.out.println("无论成功失败表演之后都调用");
    }
    @AfterReturning("performance()")
    public void applaud(){
        System.out.println("表演成功之后要鼓掌");
    }
    @AfterThrowing("performance()")
    public void demandRefund(){
        System.out.println("表演失败之后要退票");
    }
    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint joinPoint){
        try {
            /**表演之前**/
            System.out.println("环绕通知——表演之前获得座位！");
            System.out.println("环绕通知——表演之前关闭手机！");
            long start = System.currentTimeMillis();

            /** 执行被通知的方法 **/
            joinPoint.proceed();

            /**表演之后*/
            long end = System.currentTimeMillis();
            System.out.println("环绕通知——表演之后要鼓掌!");
            System.out.println("环绕通知——"+(end-start));
        } catch (Throwable throwable) {
            System.out.println("环绕通知——表演失败后我们要求退票！");
            throwable.printStackTrace();
        }
    }
}
